package org.aplombh.java.interview.newcoder;

import java.util.Scanner;

public class HJ14_字符串排序 {
    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);

        while (in.hasNextInt()) {
            int n = in.nextInt();
            String[] alls = new String[n];
            for (int i = 0; i < n; i++) {
                alls[i] = in.next();
            }
            quickSort(alls, 0, n - 1);
            for (String all : alls) {
                System.out.println(all);
            }
        }
    }

    private static void quickSort(String[] alls, int l, int r) {
        if (l >= r) return;

        int mid = l + r >> 1, i = l - 1, j = r + 1;
        String x = alls[mid];

        while (i < j) {
            while (compare(x, alls[++i]) > 0) ;
            while (compare(x, alls[--j]) < 0) ;

            if (i < j) {
                String tmp = alls[i];
                alls[i] = alls[j];
                alls[j] = tmp;
            }
        }
        quickSort(alls, l, j);
        quickSort(alls, j + 1, r);
    }

    public static int compare(String s1, String s2) {
        int l1 = s1.length();
        int l2 = s2.length();

        for (int i = 0; i < l1 && i < l2; i++) {
            if (s1.charAt(i) > s2.charAt(i))
                return 1;
            if (s1.charAt(i) < s2.charAt(i))
                return -1;
        }
        return Integer.compare(l1, l2);
    }
}
